package com.paso.sparsearray;

import java.io.*;

/**
 * 稀疏数组
 * 二维数组-》稀疏数组-》存入文件-》读取文件-》稀疏数组-》二维数组
 */
public class SparseArray {
    public static void main(String[] args) throws IOException {
        // 创建一个原始二维数组 11 * 11
        // 0表示没有棋子， 1表示黑子， 2表示蓝子
        int[][] chessArray1 = new int[11][11];
        chessArray1[1][2] = 1;
        chessArray1[2][3] = 2;
        chessArray1[3][4] = 2;
        chessArray1[4][5] = 1;
        chessArray1[5][6] = 1;

        // 遍历二维数组
        for (int[] row : chessArray1) {
            for (int data : row) {
                System.out.printf("%d\t", data);
            }
            System.out.println();
        }

        System.out.println("=============分割符==========");

        // 二维数组转稀疏数组

        //1.获取有效数据的个数
        int num = 0;
        for (int[] row : chessArray1) {
            for (int data : row) {
                if (data != 0) {
                    num++;
                }
            }
        }

        System.out.printf("有效数据个数:%d\t\n", num);


        //2.创建稀疏数组
        int[][] chessArray2 = new int[num + 1][3];

        //第一行数据
        chessArray2[0][0] = 11;
        chessArray2[0][1] = 11;
        chessArray2[0][2] = num;

        //后面的数据
        int count = 0;
        for (int i = 0; i < 11; i++) {
            for (int j = 0; j < 11; j++) {
                if (chessArray1[i][j] != 0) {
                    count++;
                    chessArray2[count][0] = i;
                    chessArray2[count][1] = j;
                    chessArray2[count][2] = chessArray1[i][j];
                }
            }
        }

        //3.打印稀疏数组
        for (int[] row : chessArray2) {
            for (int data : row) {
                System.out.printf("%d\t", data);
            }
            System.out.println();
        }


        // 把稀疏数组存入到文件中
        System.out.println("存入稀疏数组到文件~~~~");
        File file = new File("sparse.data");
        FileWriter fileWriter = new FileWriter(file);
        for (int i = 0; i < chessArray2.length; i++) {
            for (int j = 0; j < 3; j++) {
                fileWriter.write(chessArray2[i][j] + "\t");
            }
            fileWriter.write("\r\n");//回车换行
        }
        fileWriter.close();

        // 从文件读取稀疏数组
        BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
        String line; //一行数据

        int[][] chessArray10 = new int[num+1][3];

        int rowNew = 0;
        while ((line = bufferedReader.readLine()) != null) {
            String[] temp = line.split("\t");
            for (int i = 0; i < temp.length; i++) {
                chessArray10[rowNew][i] = Integer.parseInt(temp[i]);
            }
            rowNew++;
        }
        bufferedReader.close();

        System.out.println("从文件读取的稀疏数组：~~~~");
        for (int[] rows : chessArray10) {
            for (int data : rows) {
                System.out.printf("%d\t",data);
            }
            System.out.println();
        }


        System.out.println("=============分割符==========");


        // 稀疏数组转成原始数组

        //1.创建原始数组
        int[][] chessArray3 = new int[chessArray2[0][0]][chessArray2[0][1]];


        //2.插入数据
        for (int i = 1; i < chessArray2.length; i++) {
            chessArray3[chessArray2[i][0]][chessArray2[i][1]] = chessArray2[i][2];
        }

        //打印转换后的原始数组
        for (int[] rows : chessArray3) {
            for (int data : rows) {
                System.out.printf("%d\t", data);
            }
            System.out.println();
        }


    }
}
